package cn.edu.nit.sys.controller;

import cn.edu.nit.sys.config.OperationAnnotation;
import cn.edu.nit.sys.entity.Hs;
import cn.edu.nit.sys.service.SysInfoService;
import cn.edu.nit.sys.utils.R;
import cn.edu.nit.sys.utils.SysRuntimeInfo;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author xiaobai
 * @Date 2022/8/28 22:23
 * @Version 1.0.0
 */
@RestController
@RequestMapping("/sys")
public class SysInfoController {

    @Autowired
    private SysRuntimeInfo sysRuntimeInfo;

    @Autowired
    private SysInfoService sysInfoService;

    @GetMapping
//    @PreAuthorize("hasAuthority('ADMIN')")
    @OperationAnnotation(description = "获取系统信息", opType = "查询")
    public R getSysInfo() throws Exception {
        JSONObject jsonObject = new JSONObject();
        List<Hs> sysFileInfo = sysRuntimeInfo.getSysFileInfo();
        JSONObject sysInfo = sysRuntimeInfo.getSysInfo();
        JSONObject cpuInfo = sysRuntimeInfo.getCpuInfo();
        JSONObject jvmInfo = sysRuntimeInfo.getJvmInfo();
        JSONObject memory = sysRuntimeInfo.getMemory();
        JSONObject otherInfo = sysRuntimeInfo.getOtherInfo();
        jsonObject.put("sysFileInfo", sysFileInfo);
        jsonObject.put("sysInfo", sysInfo);
        jsonObject.put("cpuInfo", cpuInfo);
        jsonObject.put("jvmInfo", jvmInfo);
        jsonObject.put("memory", memory);
        jsonObject.put("otherInfo", otherInfo);
        return R.ok(jsonObject);
    }


    @GetMapping("/cpu")
//    @PreAuthorize("hasAuthority('ADMIN')")
    public R getCpuRate() {
        return sysInfoService.getCpuRate();
    }

    @GetMapping("/net")
//    @PreAuthorize("hasAuthority('ADMIN')")
    public R getNet() {
        return sysInfoService.getNet();
    }

    @GetMapping("/memory")
//    @PreAuthorize("hasAuthority('ADMIN')")
    public R getMemory() {
        return R.ok(sysRuntimeInfo.getMemory());
    }
}
